module Persistence
  ( appDataFilePath
  , loadAppData
  , dumpAppData
  ) where

import System.Directory (doesFileExist)
import Data.Aeson (encode, decode)
import qualified Data.ByteString.Lazy.Char8 as BS

import qualified DataType as DT

appDataFilePath :: FilePath
appDataFilePath = "agenda.json"

loadAppData :: FilePath -> IO (Maybe DT.AppData)
loadAppData f = do
  e <- doesFileExist f
  if e then decode <$> BS.readFile appDataFilePath else return Nothing

dumpAppData :: FilePath -> DT.AppData -> IO ()
dumpAppData f d = BS.writeFile f (encode d)
